package com.jg.framework.utils.number;

import java.text.DecimalFormat;

/**
 * Long转换工具类，主要是金额转换、文件大小转换等
 * @author ZhaoJian
 *
 */
public class LongUtil {

	/** 元 */
	public static final long YUAN = 100l;
	/** 万元 */
	public static final long WAN_YUAN = 1000000l;
	
	/**
	 * 字符串金额转成分
	 * @param price 金额
	 * @param multiplyNum 乘的倍数
	 * @return
	 */
	public static long parsePrice(String price, long multiplyNum){
		if(price != null && price.length() > 0){
			Double d = Double.valueOf(price) * multiplyNum;
			return d.longValue();
		}else{
			return 0l;
		}
	}
	
	/**
	 * 金额long转成字符串
	 * @param price 数字，单位分
	 * @param divideNum 除数
	 * @return
	 */
	public static String formatePrice(Long price, long divideNum){
		if(price == null){
			return "";
		}
		Double d = Double.valueOf(price);
		return String.valueOf(Math.round(d)/Double.valueOf(divideNum + ".0"));
	}
	
	/**
	 * 金额long转成double
	 * @param price 数字，单位分
	 * @param divideNum 除数
	 * @return
	 */
	public static double formatePriceToDouble(Long price, long divideNum){
		Double d = Double.valueOf(price);
		return Double.valueOf(String.valueOf(Math.round(d)/Double.valueOf(divideNum + ".0")));
	}
	
	public static void main(String[] args) {
		double radio = 0.3;
		int i = (int) Math.round(5*radio);
		System.out.println(i);
	}
	
	/** 
     * 返回byte的数据大小对应的文本 
     * @param size 
     * @return 
     */  
    public static String getDataSize(long size){  
        DecimalFormat formater = new DecimalFormat("####.00");  
        if(size<1024){  
            return size+"bytes";  
        }else if(size<1024*1024){  
            float kbsize = size/1024f;    
            return formater.format(kbsize)+"KB";  
        }else if(size<1024*1024*1024){  
            float mbsize = size/1024f/1024f;    
            return formater.format(mbsize)+"MB";  
        }else if(size<1024*1024*1024*1024){  
            float gbsize = size/1024f/1024f/1024f;    
            return formater.format(gbsize)+"GB";  
        }else{  
            return "size: error";  
        }  
    }
}
